﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Drawing;
using Microsoft.DirectX;

namespace NGE
{
    public class Util
    {
        public class FileLoader
        {
            public byte[] FileData;
            public int Size = 0;
            public int Loaded = 0;
            public TimeSpan TakeTime;
            DateTime startTime;
            public double Progress
            {
                get
                {
                    return (double)Loaded / (double)Size;
                }
            }
            public void Load(string fileName)
            {
                startTime = DateTime.Now;
                Loaded = 0;
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                Size = (int)fs.Length;
                FileData = new byte[Size];
                fs.BeginRead(FileData, 0, Size, new AsyncCallback(read), fs);
            }
            void read(IAsyncResult iar)
            {
                FileStream fs = (FileStream)iar.AsyncState;
                Loaded += fs.EndRead(iar);
                if (iar.IsCompleted)
                {
                    TakeTime = DateTime.Now - startTime;
                    return;
                }
                else
                {
                    fs.BeginRead(FileData, Loaded, Size - Loaded, new AsyncCallback(read), fs);
                }
            }
        }
        public static List<Vector3> GetZone(int range)
        {
            List<Vector3> rz = new List<Vector3>();
            int ymax = range * 2 + 1;
            for (int y = 0; y < ymax / 2 + 1; y++)
            {
                int xmax = y * 2 + 1;
                for (int x = 0; x < xmax; x++)
                {
                    Vector3 v3 = new Vector3(x - xmax / 2, y - ymax / 2, 0);
                    rz.Add(v3);
                }
            }
            int rzCount = rz.Count;
            for (int i = 0; i < rzCount; i++)
            {
                Vector3 v3 = rz[i];
                if (v3.Y != 0)
                {
                    v3.Y = -v3.Y;
                    rz.Add(v3);
                }
            }
            rz.Remove(new Vector3());
            return rz;
        }
        public static List<Vector3> GetZone(int min_range, int max_range)
        {
            List<Vector3> rz = new List<Vector3>();
            int ymax = max_range * 2 + 1;
            for (int y = min_range; y < ymax / 2 + 1; y++)
            {
                int xmax = y * 2 + 1;
                for (int x = 0; x < xmax; x++)
                {
                    Vector3 v3 = new Vector3(x - xmax / 2, y - ymax / 2, 0);
                    rz.Add(v3);
                }
            }
            int rzCount = rz.Count;
            for (int i = 0; i < rzCount; i++)
            {
                Vector3 v3 = rz[i];
                if (v3.Y != 0)
                {
                    v3.Y = -v3.Y;
                    rz.Add(v3);
                }
            }
            rz.Remove(new Vector3());
            return rz;
        }

        public static string[] GetTextLines(string text)
        {
            string[] texts = text.Split(new string[] { "\n", "\\r\\n", "\\n", "\r\n" }, StringSplitOptions.None);
            return texts;
        }
        public static object Clone(object obj)
        {
            byte[] buf = Util.Serialize(obj);
            return Util.Deserialize(buf);
        }
        public static Size GetTextSize(string text, int sz)
        {
            return GetTextSize(text, new Size(sz, sz));
        }
        public static Size GetTextSize(string text, Size sz)
        {
            Rectangle tsize = new Rectangle();
            {
                string[] texts = GetTextLines(text);
                int longest = 0;
                foreach (string str in texts)
                {
                    if (str.Length > longest)
                    {
                        longest = str.Length;
                    }
                }

                tsize.Width = longest * sz.Width;
                tsize.Height = texts.Length * sz.Height;

                return tsize.Size;
            }
        }

        public static Point PointAddPoint(Point p1, Point p2)
        {
            return new Point(p1.X + p2.X, p1.Y + p2.Y);
        }
        public static Point PointSubPoint(Point p1, Point p2)
        {
            return new Point(p1.X - p2.X, p1.Y - p2.Y);
        }
        public static Point PointMulInt(Point v3, int unit)
        {
            return new Point(v3.X * unit, v3.Y * unit);
        }
        public static Point PointDivInt(Point v3, int unit)
        {
            return new Point(v3.X / unit, v3.Y / unit);
        }
        public static Vector3 Vector3MulInt(Vector3 v3, int unit)
        {
            return new Vector3(v3.X * unit, v3.Y * unit, v3.Z * unit);
        }
        public static Vector3 Vector3DivInt(Vector3 v3, int unit)
        {
            return new Vector3(v3.X / unit, v3.Y / unit, v3.Z / unit);
        }
        public static Vector3 Vector3MulDouble(Vector3 v3, double unit)
        {
            return new Vector3(v3.X * (float)unit, v3.Y * (float)unit, v3.Z * (float)unit);
        }
        public static Vector3 Vector3DivDouble(Vector3 v3, double unit)
        {
            return new Vector3(v3.X / (float)unit, v3.Y / (float)unit, v3.Z / (float)unit);
        }
        public static Vector3 Point2Vector3(Point p)
        {
            return new Vector3(p.X, p.Y, 0);
        }
        public static Point Vector32Point(Vector3 v3)
        {
            return new Point((int)v3.X, (int)v3.Y);
        }
        public static bool Is2PowSize(Size size)
        {
            if (Is2Pow(size.Width) && Is2Pow(size.Height))
            {
                return true;
            }
            return false;
        }
        public static Vector3 Vector3AddVector3(Vector3 v31, Vector3 v32)
        {
            return new Vector3(v31.X + v32.X, v31.Y + v32.Y, v31.Z + v32.Z);
        }
        public static Vector3 Vector3SubVector3(Vector3 v31, Vector3 v32)
        {
            return new Vector3(v31.X - v32.X, v31.Y - v32.Y, v31.Z - v32.Z);
        }
        public static bool Is2Pow(int num)
        {
            int j = 1;
            for (int i = 0; i < 16; i++)
            {
                j <<= 1;
                if (num == j)
                {
                    return true;
                }
            }
            return false;
        }
        public static bool IsInZone(Vector3 loc, Rectangle zone)
        {
            Point p = Vector32Point(loc);
            if (p.X >= zone.X)
            {
                if (p.Y >= zone.Y)
                {
                    if (p.X < zone.Right)
                    {
                        if (p.Y < zone.Bottom)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public static bool ImageIsSame(Image img1, Image img2)
        {
            //if (img1 == null || img2 == null)
            //{
            //    return false;
            //}
            MemoryStream ms = new MemoryStream();
            img1.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            string str1 = Convert.ToBase64String(ms.ToArray());
            ms.Position = 0;
            img2.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            string str2 = Convert.ToBase64String(ms.ToArray());
            if (str1.Equals(str2))
            {
                return true;
            }
            return false;
        }
        public static bool NImageIsSame(NImage n1, NImage n2)
        {
            if (n1 == null || n2 == null)
            {
                return false;
            }
            if (n1.Width != n2.Width)
            {
                return false;
            }
            if (n1.Height != n2.Height)
            {
                return false;
            }
            if (n1.DataByte.Length != n2.DataByte.Length)
            {
                return false;
            }
            string str1 = Convert.ToBase64String(n1.DataStream.ToArray());
            string str2 = Convert.ToBase64String(n2.DataStream.ToArray());
            if (!str1.Equals(str2))
            {
                return false;
            }
            return true;
        }

        public static void EnumDir(string root,ref List<string> list)
        {
            //list = new List<string>();
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(root);
            System.IO.DirectoryInfo[] dis = di.GetDirectories();
            for (int i = 0; i < dis.Length; i++)
            {
                list.Add(dis[i].FullName);
                EnumDir(dis[i].FullName,ref list);
            }
        }
        public static void EnumFile(string root, string extName, ref List<FileIndexer> list)
        {
            //list = new List<FileIndexer>();
            DirectoryInfo di = new DirectoryInfo(root);
            FileInfo[] fis = di.GetFiles(extName);
            foreach (FileInfo fi in fis)
            {
                list.Add(new FileIndexer(fi.FullName));
            }
        }
        public static byte[] Compress(byte[] input)
        {
            System.IO.MemoryStream os = new System.IO.MemoryStream();
            zlib.ZOutputStream zos = new zlib.ZOutputStream(os, zlib.zlibConst.Z_BEST_COMPRESSION);
            zos.Write(input, 0, input.Length);
            zos.finish();
            zos.Close();
            return os.ToArray();
        }
        public static byte[] Decompress(byte[] input)
        {
            System.IO.MemoryStream os = new System.IO.MemoryStream();
            zlib.ZInputStream zis = new zlib.ZInputStream(new System.IO.MemoryStream(input));

            byte[] output = new byte[256];
            int count = 0;
            do
            {
                count = zis.read(output, 0, 256);
                if (count > 0)
                {
                    os.Write(output, 0, count);
                }
            }
            while (count > 0);
            output = os.ToArray();
            os.Close();
            return output;
        }
        public static byte[] Serialize(object obj)
        {
            if (obj == null)
            {
                return null;
            }
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            serializer.Serialize(memStream, obj);
            return memStream.ToArray();
        }
        public static object Deserialize(byte[] data)
        {
            if (data == null)
            {
                return null;
            }
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream memStream = new System.IO.MemoryStream(data);
            object newobj = deserializer.Deserialize(memStream);
            memStream.Close();
            return newobj;
        }
        public static object LoadObjectXML(string FileName, Type type)
        {
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
            object o = xs.Deserialize(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(FileName)));
            return o;
        }
        public static void SaveObjectXML(string FileName, object obj)
        {
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            FileStream fs = System.IO.File.Open(FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            xs.Serialize(fs, obj);
            fs.Close();
            fs.Dispose();
        }
        public static object LoadObject(string FileName)
        {
            if (File.Exists(FileName) == false)
            {
                return null;
            }
            return LoadObject(System.IO.File.ReadAllBytes(FileName));
        }
        public static object LoadObject(byte[] data)
        {
            data = Decompress(data);
            return Deserialize(data);
        }
        public static Size GetFitSize(Size s)
        {
            Size ss = new Size();
            int[] list = new int[] { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 };
            for (int i = 0; i < list.Length; i++)
            {
                if (s.Width < list[i])
                {
                    ss.Width = list[i];
                    break;
                }
            }
            for (int i = 0; i < list.Length; i++)
            {
                if (s.Height < list[i])
                {
                    ss.Height = list[i];
                    break;
                }
            }
            return ss;
        }
        public static void SaveObject(string FileName, object Object)
        {
            byte[] filebuff = Serialize(Object);
            filebuff = Compress(filebuff);
            System.IO.File.WriteAllBytes(FileName, filebuff);
        }

        static System.Random Random = new System.Random(Guid.NewGuid().GetHashCode());
        public static double Roll()
        {
            return Random.NextDouble();
        }
        public static int Roll(int from, int to)
        {
            return Random.Next(from, to + 1);
        }
        public static int Roll(int count)
        {
            return Random.Next(count);
        }
        public static bool RollBool()
        {
            if (Roll(1, 100) <= 50)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static Direction GetOppositeDirection(Direction dir)
        {
            {
                if (dir == Direction.U)
                {
                    return Direction.D;
                }
                else if (dir == Direction.L)
                {
                    return Direction.R;
                }
                else if (dir == Direction.D)
                {
                    return Direction.U;
                }
                else
                {
                    return Direction.L;
                }
            }
        }

        public static Color MixColor(Color targetColor, Color filterColor)
        {
            return Color.FromArgb(targetColor.ToArgb() & filterColor.ToArgb());
        }
        public static string[] SplitString(string data, int everyline)
        {
            int line = data.Length / everyline + 1;
            string[] tmp = new string[line];
            if (line > 1)
            {
                int i = 0;
                while (i < line)
                {
                    try
                    {
                        tmp[i] = data.Substring(i * everyline, everyline);
                    }
                    catch
                    {
                        tmp[i] = data.Substring(i * everyline);
                    }
                    i++;
                }
            }
            else
            {
                tmp[0] = data;
            }
            return tmp;
        }

        public static string GenFileName(string header, string ext)
        {
            int i = System.DateTime.Now.GetHashCode();
            i = System.Math.Abs(i);
            return header + i.ToString() + ext;
        }
        public static System.Drawing.PointF GetPointOnCircle(double r, double deg)
        {
            deg %= 360;
            double degb = deg;
            deg = Radian2Degree(deg);
            double x = 0, y = 0;
            y = System.Math.Sin(deg) * r;
            x = System.Math.Sqrt(r * r - y * y);
            if (degb < 90)
            {
            }
            else if (degb < 180)
            {
                x = 0 - x;
            }
            else if (degb < 270)
            {
                x = 0 - x;
                //y = 0 - y;
            }
            else
            {
                //y = 0 - y;
            }
            return new System.Drawing.PointF((float)(x), (float)(y));
        }
        public static double Radian2Degree(double rad)
        {
            return rad * System.Math.PI / 180;
        }

        public static int ARGB1555TOARGB8888(int color)
        {
            int result = 0;
            int aa = color >> 15;
            aa &= 1;
            int r = color >> 10;
            r &= 31;
            int g = color >> 5;
            g &= 31;
            int b = color;
            b &= 31;

            r <<= 3;
            g <<= 3;
            b <<= 3;

            int rr = r >> 2;
            int gg = g >> 2;
            int bb = b >> 2;

            r |= rr;
            g |= gg;
            b |= bb;

            int a = 0xff;
            if (aa == 1)
            {
                a = 0xff;
            }
            else
            {
                a = 0;
            }

            //if (r == 0 && g == 0 && b == 0)
            //{
            //    a = 0;
            //}

            result |= a << 24;
            result |= r << 16;
            result |= g << 8;
            result |= b;

            return result;
        }
        public static int ARGB4444TOARGB8888(int color)
        {
            int result = 0;
            int a = color >> 12;
            a &= 15;
            int r = color >> 8;
            r &= 15;
            int g = color >> 4;
            g &= 15;
            int b = color;
            b &= 15;

            a <<= 4;
            r <<= 4;
            g <<= 4;
            b <<= 4;

            int aa = a >> 4;
            int rr = r >> 4;
            int gg = g >> 4;
            int bb = b >> 4;

            r |= rr;
            g |= gg;
            b |= bb;

            result |= a << 24;
            result |= r << 16;
            result |= g << 8;
            result |= b;

            return result;
        }

        public static int ByteArrayContainsByteArray(byte[] src, byte[] dest)
        {
            for (int i = 0; i < src.Length; i++)
            {
                if (src[i] == dest[0])
                {
                    if (i + dest.Length <= src.Length)
                    {
                        bool passed = true;
                        for (int j = 1; j < dest.Length; j++)
                        {
                            if (src[j + i] != dest[j])
                            {
                                passed = false;
                                break;
                            }
                        }
                        if (passed)
                        {
                            return i;
                        }
                    }
                }
            }
            return -1;
        }

        public static byte[] ByteArrayCut(byte[] srcbyte, int offset, int count)
        {
            if (offset + count > srcbyte.Length)
            {
                return null;
            }
            byte[] dstbyte = new byte[count];
            int c;
            c = 0;
            while (c < count)
            {
                dstbyte[c] = srcbyte[offset + c];
                c++;
            }
            return dstbyte;
        }
        public static byte[] ByteArrayCombine(byte[] src1, byte[] src2)
        {
            if (src1 == null && src2 != null)
            {
                return src2;
            }
            else if (src2 == null && src1 != null)
            {
                return src1;
            }
            else if (src1 == null && src2 == null)
            {
                return new byte[] { };
            }
            byte[] tmp = new byte[src1.Length + src2.Length];
            for (int i = 0; i < src1.Length; i++)
            {
                tmp[i] = src1[i];
            }
            for (int i = 0; i < src2.Length; i++)
            {
                tmp[i + src1.Length] = src2[i];
            }
            return tmp;
        }
        public static float GetDistance(Vector3 f, Vector3 t)
        {
            float x = Math.Abs(f.X - t.X);
            float y = Math.Abs(f.Y - t.Y);
            return (float)Math.Sqrt(x * x + y * y);
        }
        public static Direction GetDirection(string str)
        {
            str = str.ToLower();
            Direction dir = Direction.U;
            if (str == "u")
            {
                dir = Direction.U;
            } 
            else if (str == "l")
            {
                dir = Direction.L;
            }
            else if (str == "d")
            {
                dir = Direction.D;
            }
            else if (str == "r")
            {
                dir = Direction.R;
            }
            return dir;
        }
        public static Direction GetDirection(Vector3 my, Vector3 target)
        {
            Direction dir = Direction.U;
            if (my.X == target.X && my.Y == target.Y)
            {
                //return null;
            }
            if (my.X == target.X)
            {
                if (target.Y > my.Y)
                {
                    dir = Direction.D;
                }
                else
                {
                    dir = Direction.U;
                }
            }
            else if (my.Y == target.Y)
            {
                if (target.X > my.X)
                {
                    dir = Direction.R;
                }
                else
                {
                    dir = Direction.L;
                }
            }
            return dir;
        }
        public static string GetMD5(string fileName)
        {
            string hashMD5 = string.Empty;

            //检查文件是否存在，如果文件存在则进行计算，否则返回空值
            if (System.IO.File.Exists(fileName))
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    //计算文件的MD5值
                    System.Security.Cryptography.MD5 calculator = System.Security.Cryptography.MD5.Create();
                    Byte[] buffer = calculator.ComputeHash(fs);
                    calculator.Clear();
                    //将字节数组转换成十六进制的字符串形式
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        stringBuilder.Append(buffer[i].ToString("x2"));
                    }
                    hashMD5 = stringBuilder.ToString();
                }//关闭文件流

            }//结束计算

            return hashMD5;
        }

        public static byte[] Encrypt(string src, string key)
        {
            byte[] key_bin = System.Text.Encoding.ASCII.GetBytes(key);
            byte[] key_bin2 = Compress(key_bin);

            byte len = (byte)key_bin.Length;

            byte[] header_data = new byte[key_bin2.Length + 1];
            header_data[0] = (byte)key_bin2.Length;
            for (byte i = 0; i < key_bin2.Length; i++)
            {
                header_data[i + 1] = key_bin2[i];
            }

            byte[] src_data = System.Text.Encoding.Unicode.GetBytes(src);

            for (int i = 0; i < src_data.Length; i++)
            {
                src_data[i] += key_bin[i % len];
            }


            return ByteArrayCombine(header_data, src_data);
        }

        public static string Decrypt(byte[] src_bin)
        {
            byte len = src_bin[0];
            byte[] key_bin = new byte[len];
            for (int i = 0; i < len; i++)
            {
                key_bin[i] = src_bin[i + 1];
            }
            byte len2 = len;
            key_bin = Decompress(key_bin);
            len2 = (byte)key_bin.Length;

            byte[] src_data = ByteArrayCut(src_bin, len + 1, src_bin.Length - len - 1);

            for (int i = 0; i < src_data.Length; i++)
            {
                src_data[i] -= key_bin[i % len2];
            }

            return System.Text.Encoding.Unicode.GetString(src_data);
        }




        //public static FileLoader FileLoader = new FileLoader();

        ///// <summary>
        ///// 提供用于计算指定文件哈希值的方法
        ///// <example>例如计算文件的MD5值:
        ///// <code>
        /////   String hashMd5=HashHelper.ComputeMD5("MyFile.txt");
        ///// </code>
        ///// </example>
        ///// <example>例如计算文件的CRC32值:
        ///// <code>
        /////   String hashCrc32 = HashHelper.ComputeCRC32("MyFile.txt");
        ///// </code>
        ///// </example>
        ///// <example>例如计算文件的SHA1值:
        ///// <code>
        /////   String hashSha1 =HashHelper.ComputeSHA1("MyFile.txt");
        ///// </code>
        ///// </example>
        ///// </summary>
        //public sealed class HashHelper
        //{
        //    /// <summary>
        //    ///  计算指定文件的MD5值
        //    /// </summary>
        //    /// <param name="fileName">指定文件的完全限定名称</param>
        //    /// <returns>返回值的字符串形式</returns>
        //    public static String ComputeMD5(String fileName)
        //    {
        //        String hashMD5 = String.Empty;

        //        //检查文件是否存在，如果文件存在则进行计算，否则返回空值
        //        if (System.IO.File.Exists(fileName))
        //        {
        //            using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
        //            {
        //                //计算文件的MD5值
        //                System.Security.Cryptography.MD5 calculator = System.Security.Cryptography.MD5.Create();
        //                Byte[] buffer = calculator.ComputeHash(fs);
        //                calculator.Clear();
        //                //将字节数组转换成十六进制的字符串形式
        //                StringBuilder stringBuilder = new StringBuilder();
        //                for (int i = 0; i < buffer.Length; i++)
        //                {
        //                    stringBuilder.Append(buffer[i].ToString("x2"));
        //                }
        //                hashMD5 = stringBuilder.ToString();
        //            }//关闭文件流

        //        }//结束计算

        //        return hashMD5;
        //    }//ComputeMD5

        //    /// <summary>
        //    ///  计算指定文件的CRC32值
        //    /// </summary>
        //    /// <param name="fileName">指定文件的完全限定名称</param>
        //    /// <returns>返回值的字符串形式</returns>
        //    public static String ComputeCRC32(String fileName)
        //    {
        //        String hashCRC32 = String.Empty;

        //        //检查文件是否存在，如果文件存在则进行计算，否则返回空值
        //        if (System.IO.File.Exists(fileName))
        //        {
        //            using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
        //            {
        //                //计算文件的CSC32值
        //                Crc32 calculator = new Crc32();
        //                Byte[] buffer = calculator.ComputeHash(fs);
        //                calculator.Clear();
        //                //将字节数组转换成十六进制的字符串形式
        //                StringBuilder stringBuilder = new StringBuilder();
        //                for (int i = 0; i < buffer.Length; i++)
        //                {
        //                    stringBuilder.Append(buffer[i].ToString("x2"));
        //                }

        //                hashCRC32 = stringBuilder.ToString();
        //            }//关闭文件流
        //        }
        //        return hashCRC32;
        //    }//ComputeCRC32

        //    /// <summary>
        //    ///  计算指定文件的SHA1值
        //    /// </summary>
        //    /// <param name="fileName">指定文件的完全限定名称</param>
        //    /// <returns>返回值的字符串形式</returns>
        //    public static String ComputeSHA1(String fileName)
        //    {
        //        String hashSHA1 = String.Empty;

        //        //检查文件是否存在，如果文件存在则进行计算，否则返回空值
        //        if (System.IO.File.Exists(fileName))
        //        {
        //            using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
        //            {
        //                //计算文件的SHA1值
        //                System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
        //                Byte[] buffer = calculator.ComputeHash(fs);
        //                calculator.Clear();
        //                //将字节数组转换成十六进制的字符串形式
        //                StringBuilder stringBuilder = new StringBuilder();
        //                for (int i = 0; i < buffer.Length; i++)
        //                {
        //                    stringBuilder.Append(buffer[i].ToString("x2"));
        //                }

        //                hashSHA1 = stringBuilder.ToString();

        //            }//关闭文件流
        //        }

        //        return hashSHA1;
        //    }//ComputeSHA1

        //}//end class: HashHelper

        ///// <summary>
        ///// 提供 CRC32 算法的实现
        ///// </summary>
        //public class Crc32 : System.Security.Cryptography.HashAlgorithm
        //{
        //    public const UInt32 DefaultPolynomial = 0xedb88320;
        //    public const UInt32 DefaultSeed = 0xffffffff;
        //    private UInt32 hash;
        //    private UInt32 seed;
        //    private UInt32[] table;
        //    private static UInt32[] defaultTable;
        //    public Crc32()
        //    {
        //        table = InitializeTable(DefaultPolynomial);
        //        seed = DefaultSeed;
        //        Initialize();
        //    }
        //    public Crc32(UInt32 polynomial, UInt32 seed)
        //    {
        //        table = InitializeTable(polynomial);
        //        this.seed = seed;
        //        Initialize();
        //    }
        //    public override void Initialize()
        //    {
        //        hash = seed;
        //    }
        //    protected override void HashCore(byte[] buffer, int start, int length)
        //    {
        //        hash = CalculateHash(table, hash, buffer, start, length);
        //    }
        //    protected override byte[] HashFinal()
        //    {
        //        byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
        //        this.HashValue = hashBuffer;
        //        return hashBuffer;
        //    }
        //    public static UInt32 Compute(byte[] buffer)
        //    {
        //        return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
        //    }
        //    public static UInt32 Compute(UInt32 seed, byte[] buffer)
        //    {
        //        return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
        //    }
        //    public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
        //    {
        //        return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
        //    }
        //    private static UInt32[] InitializeTable(UInt32 polynomial)
        //    {
        //        if (polynomial == DefaultPolynomial && defaultTable != null)
        //        {
        //            return defaultTable;
        //        }
        //        UInt32[] createTable = new UInt32[256];
        //        for (int i = 0; i < 256; i++)
        //        {
        //            UInt32 entry = (UInt32)i;
        //            for (int j = 0; j < 8; j++)
        //            {
        //                if ((entry & 1) == 1)
        //                    entry = (entry >> 1) ^ polynomial;
        //                else
        //                    entry = entry >> 1;
        //            }
        //            createTable[i] = entry;
        //        }
        //        if (polynomial == DefaultPolynomial)
        //        {
        //            defaultTable = createTable;
        //        }
        //        return createTable;
        //    }
        //    private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
        //    {
        //        UInt32 crc = seed;
        //        for (int i = start; i < size; i++)
        //        {
        //            unchecked
        //            {
        //                crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
        //            }
        //        }
        //        return crc;
        //    }
        //    private byte[] UInt32ToBigEndianBytes(UInt32 x)
        //    {
        //        return new byte[] { (byte)((x >> 24) & 0xff), (byte)((x >> 16) & 0xff), (byte)((x >> 8) & 0xff), (byte)(x & 0xff) };
        //    }
        //}//end class: Crc32

    }

}
